<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>掷骰子</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .button{
            margin: 10px auto;
            width: 500px;
            height: 30px;
        }
        button {
            margin-left: 50px;
            width: 100px;
            height: 30px;
        }
        div ul {
            position: relative;
            margin: 200px 650px;
            width: 100px;
            height: 100px;
            /* 定义3d模式 */
            transform-style: preserve-3d;
            /* 初始角度 */
            transform: rotateX(60deg) rotateY(30deg) rotateZ(30deg);
            /* 过渡时间 */
            transition: all 0.2s;
        }

        li {
            list-style: none;
            position: absolute;
            width: 100px;
            height: 100px;
            border: 1px solid black;
            background-color: aqua;
            background-size: 100% 100%;
            border-radius: 30px;
            text-align: center;
            line-height: 100px;
        }
        /* 每一面旋转不同角度后偏移 */
        li:first-child {
            background-image: url(one.jpg);
        }

        li:nth-child(2) {
            background-image: url(two.jpg);
            transform: rotateX(90deg) translateY(-50px) translateZ(50px);
        }

        li:nth-child(4) {
            background-image: url(four.jpg);
            background-color: aquamarine;
            transform: rotateX(90deg) translateY(-50px) translateZ(-50px);
        }

        li:nth-child(3) {
            background-image: url(three.jpg);
            background-color: bisque;
            transform: rotateY(90deg) translateZ(-50px) translateX(50px);
        }

        li:nth-child(5) {
            background-image: url(five.jpg);
            background-color: pink;
            transform: rotateY(90deg) translateZ(50px) translateX(50px);
        }

        li:nth-child(6) {
            background-image: url(six.jpg);
            background-color: green;
            transform: translateZ(-100px) rotateX(180deg);
        }
        /* 骰蛊 */
        .tougu {
            float: left;
            position: absolute;
            z-index: 9;
            width: 300px;
            height: 450px;
            background-image: url(tougu.jpg);
            background-size: 100% 100%;
            /* opacity: 0.5; */
            transition: 1s;
            /* transform: translate(550px); */
        }

    
    </style>
</head>

<body>
    
    <div>
        <div class="button">
            <button onclick="f1()">开始</button>
            <button onclick="f2()">停止</button>

        </div>
        <!-- 骰蛊 -->
        <div class="tougu" id="tougu">

        </div>
        <!-- 绘制骰子 -->
        <ul id="scroll">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
        </ul>

        <!-- js代码 -->
        <script>
            var flag = true;
            function f1() {
                if(flag) {
                    document.getElementById('tougu').style.transform = 'translate(550px)';
                    // 周期调用
                    n = setInterval(() => {
                        var i = Math.floor(Math.random() * 1000);   //生成随机数定义转换角度
                        var j = Math.floor(Math.random() * 1000);
                        var k = Math.floor(Math.random() * 1000);
                        document.getElementById('scroll').style.transform = 'rotateX(60deg) rotateY(' + j + 'deg) rotateZ(' + k + 'deg)';
                    }, 100);
                    // 清除标志位，避免重复触发onclick
                    flag = false;
                } 
            }
            function f2() {
                // 随机数定义结果
                var m = Math.floor(Math.random() * 6) + 1;
                // 清除周期函数
                clearInterval(n);
                // 打开标志位
                flag = true;
                document.getElementById('tougu').style.transform = 'translate(0)';
                // 根据随机数，计算角度后，让骰子旋转角度
                switch (m) {
                    case 1:
                        document.getElementById('scroll').style.transform = 'rotateX(360deg) ';
                        break;
                    case 2:
                        document.getElementById('scroll').style.transform = 'rotateX(270deg) ';
                        break;
                    case 3:
                        document.getElementById('scroll').style.transform = 'rotateY(90deg) ';
                        break;
                    case 4:
                        document.getElementById('scroll').style.transform = 'rotateX(90deg) ';
                        break;
                    case 5:
                        document.getElementById('scroll').style.transform = 'rotateY(270deg) ';
                        break;
                    case 6:
                        document.getElementById('scroll').style.transform = 'rotateX(180deg) ';
                        break;
                }
            }
        </script>
    </div>
</body>

</html>
